#include "mprpcconfig.h"
#include <iostream>
#include <string>
void MprpcConfig::LoadConfigFile(const char* config_file)
{
    FILE* pf=fopen(config_file,"r");
    if(pf==nullptr)
    {
        std::cout<<"config_file is invalid\n";
        exit(EXIT_FAILURE);
    }
    while(!feof(pf))
    {
        char buf[512]={0};
        fgets(buf,512,pf);

        std::string src_buf(buf);
        Trim(src_buf);
        if(src_buf[0]=='#' || src_buf.empty())
        {
            continue;
        }
        int idx=src_buf.find('=');
        if(idx==-1)
        {
            continue;
        }
        std::string key;
        std::string value;
        key=src_buf.substr(0,idx);
        Trim(key);
        value=src_buf.substr(idx+1,src_buf.size()-idx);
        int endidx=src_buf.find('\n',idx);
        if(endidx!=-1)
        {
            value=src_buf.substr(idx+1,endidx-idx-1);
        }
        Trim(value);
        m_configMap.insert({key,value});
    }
}
std::string MprpcConfig::Load(const std::string& key)
{
    auto it=m_configMap.find(key);
    if(it==m_configMap.end())
    {
        return "";
    }
    return m_configMap[key];
}

void MprpcConfig::Trim(std::string& src_buf)
{
    int idx=src_buf.find_first_not_of(' ');
    if(idx!=1)
    {
        src_buf==src_buf.substr(idx,src_buf.size()-idx);
    }
    idx=src_buf.find_last_not_of(' ');
    if(idx!=-1)
    {
        src_buf=src_buf.substr(0,idx+1);
    }
}